<?php
namespace app\api\controller;

use app\BaseController;
use app\common\OssTool;
use app\model\Shop as SelfModel;
use app\model\Member;
use app\model\ShopType;

use think\facade\Cookie;
use think\Response;
use think\Request;
class Shop extends Base
{


    public function initialize()
    {
        parent::initialize();
        $this->model = "\app\model\Shop";
        $this->list_rows = 10;
        $this->index = ['id','member_id','member_mobile','title','single_pic','type_id','type_title','prov','city','area','audit_status','create_time'];
        $this->all = ['id','member_id','member_mobile','title','single_pic','type_id','type_title','prov','city','area','audit_status','create_time'];
        $this->my = ['id','member_id','member_mobile','title','single_pic','type_id','type_title','prov','city','area','audit_status','create_time'];
        $this->view = ['id','member_id','member_mobile','title','single_pic','type_id','type_title','prov','city','area','audit_status','create_time'];
        $this->msg = '门店列表';
        
        
    }
    public function upload_one()
    {

            return json(['code' => 200,'msg'=>'图片上传成功','img' => uploadOne('file')]);


    }

    //sales price distance people_avg
    public function index(Request $request)
    {
        $order = ['distance' => 'asc'];
        if(empty($request->list_rows)) $request->list_rows=100;
        if($request->order == '2'){
            $order = ['distance' => 'asc'];
        }
        for ($i=0;$i<=20;$i++){
            //SelfModel::create(['title'=>$i]);
        }
        $data = SelfModel::withSearch(array_keys($request->param), $request->param)
        ->where([['audit_status', '=', 2]])
//        ->order([ 'id' => 'desc'])
            ->field('id,title,create_time,lng,lat,single_pic,ROUND(
        6378.138 * 2 * ASIN(
            SQRT(
                POW(
                    SIN(
                        (
                            '.$request->lat.' * PI() / 180 - lat * PI() / 180
                        ) / 2
                    ),
                    2
                ) + COS('.$request->lat.' * PI() / 180) * COS(lat * PI() / 180) * POW(
                    SIN(
                        (
                            '.$request->lng.' * PI() / 180 - lng * PI() / 180
                        ) / 2
                    ),
                    2
                )
            )
        ) * 1000
    ) AS distance')->order($order)
            ->paginate($request->list_rows);

        return json(['code' => 200, 'message' => '列表', 'data' => $data,'sql'=>SelfModel::getlastsql()]);
    }

    public function all(Request $request)
    {
        $data = SelfModel::withSearch(array_keys($request->param), $request->param)
        ->where([['status', '=', 1]])
        ->order(['sort' => 'asc', 'id' => 'desc'])
        ->select();
        return json(['code' => 200, 'message' => '全部(无分页)', 'data' => $data]);
    }



    public function create(Request $request)
    {
        $ret = SelfModel::create($request->param);
        if ($ret) {
        return json(['code' => 200, 'message' => '信息发布成功']);
        }
        return json(['code' => 400, 'message' => '详情']);
    }

    public function update(Request $request)
    {
        $info = SelfModel::find($request->id);
        $ret = $info->save($this->param);
        if ($ret !== false) {
        return json(['code' => 200, 'message' => '修改成功']);
        }
        return json(['code' => 200, 'message' => '失败']);
    }


    public function destory(Request $request)
    {
        $ret = SelfModel::where('id','in',$request->id)->update(['delete_time' => date('Y-m-d H:i:s')]);
        if ($ret) return json(['code' => 200, 'message' => '删除成功']);
        return json(['code' => 400, 'message' => '失败']);
    }

    public function view(Request $request)
    {
        $data = SelfModel::find($request->id);
        $data['content'] = str_replace('<img','<img style="width: 100%;!important;display:block;max-width:100%;text-align:center"',$data['content']);
        return json(['code' => 200, 'message' => '详情', 'data' => $data]);
    }

    public function html(Request $request)
    {
        $data = SelfModel::where('id', $request->id)->value($request->field);
        $content = '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';
        $content .= '<style> img {vertical-align:middle; width:auto; max-width:100%;} </style>';
        $content .= $data;
        return Response::create($content, 'html');
    }

    public function json(Request $request)
    {
        $data = SelfModel::where('id', $request->id)->value($request->field);
        $data = str_replace('src="', 'src="' . $request->domain(), $data);
        return json($data);
    }

    public function my(Request $request)
    {
        $data = SelfModel::where('member_id', $request->member_id)->paginate(20);
        return json(['code' => 200, 'message' => '发布列表', 'data' => $data]);
    }




}
